package org.bot.plugin.window.tool;

import org.springframework.stereotype.Component;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

/**
 * @author 沧月
 * @create 0013 2023/10/13 10:44
 * @description <description class purpose>
 */

@Component
public class WindowOperate {

    private Robot robot;

    private Robot getRobot() {
        if (robot == null) {
            try {
                this.robot = new Robot();
                return robot;
            } catch (AWTException e) {
                e.printStackTrace();
            }
        }
        return robot;
    }

    /**
     * @author: 沧月
     * @date: 0014 2023/10/14 19:25
     * @param: [x, y]
     * @return: void
     * @description: 模拟点击事件
     **/
    public boolean click(int x, int y) {
        Robot robot = this.getRobot();
        robot.mouseMove(x, y);
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        return true;
    }


    /**
     * @author: 沧月
     * @date: 0014 2023/10/14 19:48
     * @param: [cmd]
     * @return: boolean
     * @description: 模拟用户键盘事件
     **/
    public boolean keyboard(String cmd) {
        if (cmd == null || cmd.length() == 0) {
            return false;
        }
        Robot robot = this.getRobot();
        String[] array = cmd.split("\\+");
        //按下
        for (String k : array) {
            switch (k.toUpperCase()) {
                case "CTRL":
                    robot.keyPress(KeyEvent.VK_CONTROL);
                    break;
                case "SHIFT":
                    robot.keyPress(KeyEvent.VK_SHIFT);
                    break;
                case "ALT":
                    robot.keyPress(KeyEvent.VK_ALT);
                    break;
                case "ENTER":
                    robot.keyPress(KeyEvent.VK_ENTER);
                    break;
                default:
                    char keyCode = k.charAt(0);
                    robot.keyPress(keyCode);
            }
        }

        //释放
        for (String k : array) {
            switch (k.toUpperCase()) {
                case "CTRL":
                    robot.keyRelease(KeyEvent.VK_CONTROL);
                    break;
                case "SHIFT":
                    robot.keyRelease(KeyEvent.VK_SHIFT);
                    break;
                case "ALT":
                    robot.keyRelease(KeyEvent.VK_ALT);
                    break;
                case "ENTER":
                    robot.keyRelease(KeyEvent.VK_ENTER);
                    break;
                default:
                    char keyCode = k.charAt(0);
                    robot.keyRelease(keyCode);
            }
        }
        return true;
    }

    /**
     * @author: 沧月
     * @date: 0014 2023/10/14 19:48
     * @param: []
     * @return: boolean
     * @description: 拷贝内容到剪切板
     **/
    public boolean copy(String content) {
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        StringSelection selection = new StringSelection(content);
        clipboard.setContents(selection, null);
        return true;
    }
}
